package com.android.antelopeapp.network;

import android.os.Bundle;
import android.os.Message;

import com.android.antelopeapp.activity.BaseActivity;
import com.android.antelopeapp.app.UserCustom;
import com.android.antelopeapp.constant.ApiConfig;
import com.android.antelopeapp.constant.MsgConfig;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lyhw.model.Comment;
import com.lyhw.model.beanwraper.BaseWraper;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by phk on 15-4-5.
 */
public class ApiManager {
    public static String TAG=ApiManager.class.getName();
    public static Gson GSON = new Gson();

    /**给文章添加评论
     *
     * */
    public static void addComment(final BaseActivity bacty,final String ra_id, final String content){
        GsonRequest<BaseWraper> grequest = new GsonRequest<BaseWraper>
                (Request.Method.POST,ApiConfig.REQUEST_APPEND_COMMENT,BaseWraper.class,
                        null,
                        new Response.Listener<BaseWraper>(){
                            @Override
                            public void onResponse(BaseWraper response) {
                                Message msg = new Message();
                                Bundle data = new Bundle();
                                data.putString(BaseActivity.MSG_TOAST_DATA, MsgConfig.COMMET_OK);
                                msg.setData(data);
                                bacty.getmHandler().sendMessage(msg);
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Message msg = new Message();
                                Bundle data = new Bundle();
                                data.putString(BaseActivity.MSG_TOAST_DATA, MsgConfig.COMMET_FAILURE);
                                msg.setData(data);
                                bacty.getmHandler().sendMessage(msg);
                            }
                        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                Comment comment = new Comment();
                comment.setAuthorid(UserCustom.getInstance().getUserId());
                comment.setContent(content);
                comment.setObjectid(ra_id);
                map.put(ApiConfig.RA_COMMENT_JSON,GSON.toJson(comment));
                return map;
            }
        };
        NetworkManager.getInstance(bacty).getRequestQueue().add(grequest);
    }
}
